package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.DealGroupUserInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <Description>收入归集集团加入,退出用户模块<br>
 * @author chenkaiwen<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020-11-25<br>
 * @see com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module.DealGroupUserInfoAttrModuleHandler<br>
 */
@Service
public class DealGroupUserInfoAttrModuleHandler extends AbstractModuleHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        SceneNewOfferSeneVO sceneVO = ((SceneNewOfferSeneVO) context.getSceneVo());
        String moduleId = sceneModule.getKiteModule().getModuleId();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        DealGroupUserInfoVO dealGroupUserInfoVO = new DealGroupUserInfoVO();
        List<KiteAttr> searchAttrs = querySceneInstService.listCommonAttrsByObjIdAndModuleId(sceneInstId, "1", moduleId);
        List<KiteAttr> tableAttr = querySceneInstService.listCommonAttrsByObjIdAndModuleId(sceneInstId, "2", moduleId);
        List tableColList = new ArrayList();
        for(KiteAttr kiteAttr :tableAttr){
            Map tableCol = new HashMap();
            tableCol.put("dataIndex", kiteAttr.getFieldName());
            tableCol.put("title", kiteAttr.getCname());
            tableColList.add(tableCol);
        }

        Map<String ,Object> searchAttrMap = new HashMap();
        searchAttrMap.put("searchAttrs",searchAttrs);

        Map<String ,Object> tableColMap = new HashMap<>();
        tableColMap.put("tableColList",tableColList);

        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String serviceOfferId = kiteSceneInst.getServiceOfferId();
        List<Map<String, Object>> tableValueList = new ArrayList<>();
        if(KiteStringUtils.equals(LocalServiceOfferConsts.SERVICE_OFFER_COLLECTION_GROUP_ADD,serviceOfferId)){
             searchAttrMap.put("layoutPage","待加入用户查询");
             tableColMap.put("layoutPage","待提交列表");
             tableValueList = querySceneInstService.getExtParameterValue(sceneInstId, "addGroupUserInfos");
        }
        if(KiteStringUtils.equals(LocalServiceOfferConsts.SERVICE_OFFER_COLLECTION_GROUP_DEL,serviceOfferId)){
             searchAttrMap.put("layoutPage","查询条件");
             tableColMap.put("layoutPage","查询结果");
             tableValueList = querySceneInstService.getExtParameterValue(sceneInstId, "delGroupUserInfos");
        }
        tableColMap.put("isEdit","T");
        tableColMap.put("tableValueList",tableValueList);
        dealGroupUserInfoVO.setSearchAttrMap(searchAttrMap);
        dealGroupUserInfoVO.setTableColMap(tableColMap);
//        dealGroupUserInfoVO.setTableValueList(tableValueList);
        sceneVO.setDealGroupUserInfoVO(dealGroupUserInfoVO);
        return Collections.singletonList(dealGroupUserInfoVO);
    }

    @Override
    public List<? extends ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        SceneNewOfferSeneVO sceneVO = ((SceneNewOfferSeneVO) context.getSceneVo());
        String moduleId = sceneModule.getKiteModule().getModuleId();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        DealGroupUserInfoVO dealGroupUserInfoVO = new DealGroupUserInfoVO();
        List<KiteAttr> searchAttrs = querySceneInstService.listCommonAttrsByObjIdAndModuleId(sceneInstId, "1", moduleId);
        List<KiteAttr> tableAttr = querySceneInstService.listCommonAttrsByObjIdAndModuleId(sceneInstId, "2", moduleId);
        List tableColList = new ArrayList();
        for(KiteAttr kiteAttr :tableAttr){
            Map tableCol = new HashMap();
            tableCol.put("dataIndex", kiteAttr.getFieldName());
            tableCol.put("title", kiteAttr.getCname());
            tableColList.add(tableCol);
        }

        Map<String ,Object> searchAttrMap = new HashMap();
        searchAttrMap.put("searchAttrs",searchAttrs);

        Map<String ,Object> tableColMap = new HashMap<>();
        tableColMap.put("tableColList",tableColList);

        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String serviceOfferId = kiteSceneInst.getServiceOfferId();
        List<Map<String, Object>> tableValueList = new ArrayList<>();
        if(KiteStringUtils.equals(LocalServiceOfferConsts.SERVICE_OFFER_COLLECTION_GROUP_ADD,serviceOfferId)){
            searchAttrMap.put("layoutPage","待加入用户查询");
            tableColMap.put("layoutPage","待提交列表");
            tableValueList = querySceneInstService.getExtParameterValue(sceneInstId, "addGroupUserInfos");
        }
        if(KiteStringUtils.equals(LocalServiceOfferConsts.SERVICE_OFFER_COLLECTION_GROUP_DEL,serviceOfferId)){
            searchAttrMap.put("layoutPage","查询条件");
            tableColMap.put("layoutPage","查询结果");
            tableValueList = querySceneInstService.getExtParameterValue(sceneInstId, "delGroupUserInfos");
        }
        tableColMap.put("isEdit","F");
        tableColMap.put("tableValueList",tableValueList);
//        dealGroupUserInfoVO.setSearchAttrMap(searchAttrMap);
        dealGroupUserInfoVO.setTableColMap(tableColMap);
//        dealGroupUserInfoVO.setTableValueList(tableValueList);
        sceneVO.setDealGroupUserInfoVO(dealGroupUserInfoVO);
        return Collections.singletonList(dealGroupUserInfoVO);
    }
}
